package com.pig4cloud.pigx.common.security.service;

import com.pig4cloud.pigx.common.security.entity.SysUser;
import com.pig4cloud.pigx.common.security.entity.UserInfo;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 * @description:
 * @Author 兔子不吃窝边曹
 * @create 2022/7/15 15:06
 **/
@Slf4j
@RequiredArgsConstructor
public class PigxDefaultUserDetailsServiceImpl implements PigxUserDetailsService {

    /**
     * 用户密码登录
     *
     * @param username 用户名
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    @SneakyThrows
    public UserDetails loadUserByUsername(String username) {

        // 调用查询用户信息接口
        // 封装测试数据
        UserInfo userInfo = new UserInfo();
        SysUser sysUser = new SysUser();
        sysUser.setUserId(1547139902912692226L);
        sysUser.setNickname("大佬味的小男孩");
        sysUser.setUsername("大佬味的小男孩");
        sysUser.setPassword("$2a$10$naALfIlVnoXUL76YV93lU.47Fs.Zi6lR7SZ4UsY7dm.p1nLcHZf7u");
        sysUser.setDelFlag("0");
        sysUser.setLockFlag("0");
        sysUser.setPhone("16670804533");
        sysUser.setAvatar("https://air-lxzn-v1.oss-cn-beijing.aliyuncs.com/upload/2022-07-08/c905ec00-7309-4ef4-8598-ac5573266df0.jpeg");
        sysUser.setDeptId(99999L);
        sysUser.setTenantId(1L);
        userInfo.setSysUser(sysUser);
        return getUserDetails(userInfo);
    }

    @Override
    public UserDetails loadUserBySocial(String mobile) throws UsernameNotFoundException {
        // feign远程调用：根据mobile查找用户信息

        // 根据查找到的用户信息判断用户是否被冻结，是否禁用，可以做一些逻辑判断

        UserInfo userInfo = new UserInfo();
        SysUser sysUser = new SysUser();
        sysUser.setUserId(1547139902912692226L);
        sysUser.setNickname("大佬味的小男孩");
        sysUser.setUsername("大佬味的小男孩");
        sysUser.setPassword("$2a$10$naALfIlVnoXUL76YV93lU.47Fs.Zi6lR7SZ4UsY7dm.p1nLcHZf7u");
        sysUser.setDelFlag("0");
        sysUser.setLockFlag("0");
        sysUser.setPhone("16670804533");
        sysUser.setAvatar("https://air-lxzn-v1.oss-cn-beijing.aliyuncs.com/upload/2022-07-08/c905ec00-7309-4ef4-8598-ac5573266df0.jpeg");
        sysUser.setDeptId(99999L);
        sysUser.setTenantId(1L);
        userInfo.setSysUser(sysUser);
        return getUserDetails(userInfo);
    }

    @Override
    public int getOrder() {
        return Integer.MIN_VALUE;
    }

}
